<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>2.5.2rest参数</title>
    </head>
    <body>
        <script>
            function calculate(method,...nums){
                let result;
                if(!method || nums.length == 0){
                    alert('请传入参数');
                    return;
                }
                if(method == '*' ){
                    result = 1;
                    for(let i =0;i<nums.length;i++){
                        result *= nums[i];
                    }
                }else if(method == '+'){
                    result=0;
                    for(let i=0;i<nums.length;i++){
                        result += nums[i];
                    }
                }
                alert(result);
            }
            calculate("+",1,2);//3
            calculate("+",1,2,3,4,5);//15
            calculate("*",1,2,3);//6
            calculate("*",1,2,3,4,5);//120

        </script>
    </body>
</html>